const cloud = require('wx-server-sdk');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {

  const wxContext = cloud.getWXContext()
  const notice=event.notice
  const user = await db.collection("user").where({
    openid:wxContext.OPENID,
  }).get()
console.log(user)
  if(user.data.length == 0)
  {
    return {
      code:401,
      data:{},
      msg:"have not login"
   }
  }
  
  if(user.data[0].status != 0 && user.data[0].status != 1)
  {
    return {
      code:401,
		 data:{},
		 msg:"no privilege"
    }
  }
  const delResult = await db.collection("notice").where({
    ababa: null
  }).remove()

  const addResult = await db.collection("notice").add({
    data:{
      message:notice
    }
  })
  return {
    delResult:delResult,
    addResult:addResult,
    retult:"success"
  }
  
};
